﻿#include "CContent.h"

CContent::CContent(void) {

}

CContent::~CContent(void) {

}

CContent::CContent(LPDIRECT3DDEVICE9 device) {
	this->d3ddev = device;
}

LPDIRECT3DTEXTURE9 CContent::LoadTexture(string path) {
	LPDIRECT3DTEXTURE9 texture = NULL;
	D3DXIMAGE_INFO info;
	HRESULT hr;
	ZeroMemory(&info,sizeof(info));
	// lấy thông tin hình ảnh
	hr = D3DXGetImageInfoFromFile(path.c_str(),&info);
	if(hr != D3D_OK) {
		return NULL;
	}
	// tạo texture
	/*hr = D3DXCreateTextureFromFileEx(this->d3ddev,path.c_str(),info.Width,info.Height,1,D3DUSAGE_DYNAMIC,
									D3DFMT_UNKNOWN,D3DPOOL_DEFAULT,D3DX_DEFAULT,D3DX_DEFAULT,D3DCOLOR_XRGB(255,255,255),
									&info,NULL,&texture);*/
	hr = D3DXCreateTextureFromFileEx(this->d3ddev,path.c_str(),info.Width,info.Height,0,0,
		D3DFMT_UNKNOWN,D3DPOOL_DEFAULT,D3DX_DEFAULT,D3DX_DEFAULT,D3DCOLOR_XRGB(0,0,0),
		NULL,NULL,&texture);
	if(hr != D3D_OK) {
		return NULL;
	}
	return texture;
}

LPDIRECT3DSURFACE9 CContent::LoadSurface(string path) {
	LPDIRECT3DSURFACE9 surface = NULL;
	D3DXIMAGE_INFO info;
	HRESULT hr;
	ZeroMemory(&info,sizeof(info));
	hr = D3DXGetImageInfoFromFile(path.c_str(),&info);
	if(hr != D3D_OK) {
		return NULL;
	}
	hr = this->d3ddev->CreateOffscreenPlainSurface(info.Width,info.Height,D3DFMT_UNKNOWN,D3DPOOL_DEFAULT,&surface,NULL);
	if(hr != D3D_OK) {
		return NULL;
	}
	// nạp hình ảnh lên surface đã tạo ra
	hr = D3DXLoadSurfaceFromFile(surface,NULL,NULL,path.c_str(),NULL,D3DX_DEFAULT,D3DCOLOR_XRGB(0,0,0),&info);
	if(hr != D3D_OK) {
		return NULL;
	}
	return surface;
}
